
const websocket = require("./websocket")
const dataHandler = require("./data_handler")
const db = require("./db")
class Controller {
    constructor() {
        this.data = null // Map
    }

    onMessage(data) {
        const json = JSON.parse(data);
        if (this[json.type]) {
            this[json.type](json);
        }
    }

    async setData(data) {
        this.data = data
    }

    async FULL_DATA() {
        if (this.data) {
            const data = [...this.data.values()]
            dataHandler.update_ps3838_data(data);
        }
    }

    async sleep(ms) {
        return new Promise(resolve => setTimeout(resolve, ms));
    }

    async odds_history(msg) {
        let id = msg.data
        let result = db.getByMatchId(id);
        websocket.send({
            type: "odds_history",
            data: result
        })
    }

    async red_history(msg) {
        let result = db.getRedHistory();
        websocket.send({
            type: "red_history",
            data: result
        })
    }

    async change_status(msg) {
        if (msg.data == "OPEN") {
            global.PS_OPEN_HAND = true
            console.info("开启手动请求平博")
        } else {
            global.PS_OPEN_HAND = false
            console.info("关闭手动请求平博")
        }
    }

    async delete_red_history(msg) {
        let id = msg.data
        db.delRedHistory(id);
    }

}


module.exports = Controller;